package com.activity.fragment;

import android.os.Bundle;
import android.view.View;

import com.activity.App;
import com.activity.adapter.AttanceMember1Adapter;

import com.activity.adapter.MonthAdapter;
import com.activity.bean.EmployerBean;
import com.activity.bean.UserBean;
import com.activity.shop.MemberSheetActivity;
import com.api.param.RecordPageParam;
import com.api.param.StoreParam;
import com.api.viewModel.StoreVM;
import com.bigkoo.pickerview.listener.OnTimeSelectListener;
import com.bigkoo.pickerview.view.TimePickerView;
import com.donkingliang.groupedadapter.adapter.GroupedRecyclerViewAdapter;
import com.donkingliang.groupedadapter.holder.BaseViewHolder;
import com.example.apple.ddhr_new.activity.widget.Dialog;
import com.example.apple.ddhr_new.utils.ActivityUtil;
import com.example.apple.ddhr_new.utils.ToastUtil;
import com.google.gson.Gson;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class MemberFragment extends BaseListViewFragment<StoreVM> {

    public int type;
    private RecordPageParam pageParam=new RecordPageParam();
    private AttanceMember1Adapter attanceMemberAdapter ;
    @Override
    public void createView() {
        super.createView();



        pageParam.storeno = App.getInstance().userstoreinfoBean.getStoreno();

        attanceMemberAdapter = new AttanceMember1Adapter(mContext);

        recycleView.setAdapter(attanceMemberAdapter);


        attanceMemberAdapter.setOnChildClickListener(new GroupedRecyclerViewAdapter.OnChildClickListener() {
            @Override
            public void onChildClick(GroupedRecyclerViewAdapter adapter, BaseViewHolder holder, int groupPosition, int childPosition) {

                UserBean employerBean = attanceMemberAdapter.getData().get(childPosition);

                Gson gson = new Gson();
                Bundle bundle = new Bundle();
                bundle.putString("param",gson.toJson(employerBean));
                bundle.putString("page",gson.toJson(pageParam));
                bundle.putInt("type",type);
                ActivityUtil.startActivity(mContext, MemberSheetActivity.class,bundle);


            }
        });
        attanceMemberAdapter.monthListener = new MonthAdapter.MonthListener() {
            @Override
            public void beginTimeClick() {
                Calendar min = Calendar.getInstance();
                Calendar max = Calendar.getInstance();
                Calendar selectDate = Calendar.getInstance();
                min.set(1900,1,1);
                max.setTime(new Date());

                TimePickerView builder = Dialog.popTime(mContext,min,max, new OnTimeSelectListener() {
                    @Override
                    public void onTimeSelect(Date date, View v) {
                        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
                        String dateString = simpleDateFormat.format(date);
                        attanceMemberAdapter.beginTime = dateString;
                        pageParam.startworktime=dateString;
                        attanceMemberAdapter.notifyDataChanged();


                    }
                });


                // 如果不设置的话，默认是系统时间*/


                if(attanceMemberAdapter.beginTime==null){
                    selectDate.setTime(new Date());
                }else
                {
                    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
                    try {
                        selectDate.setTime(simpleDateFormat.parse(attanceMemberAdapter.beginTime));
                    } catch (ParseException e) {

                    }
                }
                builder.setDate(selectDate);
                builder.show();
            }

            @Override
            public void endTimeClick() {
                Calendar min = Calendar.getInstance();
                Calendar max = Calendar.getInstance();
                Calendar selectDate = Calendar.getInstance();
                min.set(1900,1,1);
                max.setTime(new Date());

                TimePickerView builder = Dialog.popTime(mContext,min,max, new OnTimeSelectListener() {
                    @Override
                    public void onTimeSelect(Date date, View v) {
                        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
                        String dateString = simpleDateFormat.format(date);
                        attanceMemberAdapter.endTime = dateString;
                        pageParam.endworktime=dateString;

                        attanceMemberAdapter.notifyDataChanged();


                    }
                });

                // 如果不设置的话，默认是系统时间*/


                if(attanceMemberAdapter.endTime==null){
                    selectDate.setTime(new Date());
                }else
                {
                    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
                    try {
                        selectDate.setTime(simpleDateFormat.parse(attanceMemberAdapter.endTime));
                    } catch (ParseException e) {

                    }
                }
                builder.setDate(selectDate);
                builder.show();
            }

            @Override
            public void queryClick() {

                if(pageParam.startworktime==null){

                    ToastUtil.toast(mContext,"请选择开始时间");
                    return;
                }
                if(pageParam.endworktime==null){

                    ToastUtil.toast(mContext,"请选择结束时间");
                    return;
                }

                headerFreshing();
            }
        };

        viewModel.checkMember.observe(this,data->{

            if(data.isHeader){

                attanceMemberAdapter.getData().clear();
                attanceMemberAdapter.getData().addAll(data.getResult());

                headerFreshEnd();
            }else
            {
                attanceMemberAdapter.getData().addAll(data.getResult());
                if(data.getResult().size()>0){
                    footerFreshEnd();
                }else
                {
                    refreshLayout.finishLoadMoreWithNoMoreData();
                }
            }
            attanceMemberAdapter.notifyDataSetChanged();

            if(attanceMemberAdapter.getData().size()>0){
                showEmpty(false);
            }else {
                showEmpty(true);
            }


        });
        viewModel.checkMemberError.observe(this,res->{
            if(res){
                headerFreshEnd();
            }else {
                pageParam.pagenum = (Integer.valueOf(pageParam.pagenum)-1)+"";
                footerFreshEnd();
            }
        });
//        headerFreshing();

    }

    @Override
    protected void headerFreshing() {
        pageParam.pagenum ="1";

        refreshLayout.resetNoMoreData();
        addSubscription(viewModel.getCheckMember(pageParam,true,type));
    }

    @Override
    protected void footerFreshing() {
        pageParam.pagenum = (Integer.valueOf(pageParam.pagenum)+1)+"";

        addSubscription(viewModel.getCheckMember(pageParam,false,type));
    }
}
